package util.yahoo;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageHeaderDigester {

	private List rawHeaders = null ;
	private static final Class thisClass = MessageHeaderDigester.class ;
	
	public static void main(String[] args) {
		String str = 
		"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><GetMessageRawHeaderResponse total=\"1\"><rawheaders>From PCQuest Fri Nov  3 23:42:00 2006"+
		"X-Apparently-To: vijaydharap@yahoo.co.in via 66.196.97.103; Sun, 05 Nov 2006 06:30:32 +0530"+
		"X-Originating-IP: [67.18.7.74]"+
		"Return-Path: &lt;pcqrse1@cybermedia.co.in&gt;"+
		"Authentication-Results: mta129.mail.in.yahoo.com  from=cybermedia.co.in; domainkeys=neutral (no sig)"+
		"Received: from 67.18.7.74  (EHLO cyberastro.org) (67.18.7.74)"+
		"  by mta129.mail.in.yahoo.com with SMTP; Sun, 05 Nov 2006 06:30:32 +0530"+
		"Received: from cybermedia.co.in [10.0.0.2] by cyberastro.org with ESMTP"+
		"  (SMTPD32-8.05) id A44821390062; Sat, 04 Nov 2006 01:42:00 -0600"+
		"Message-ID: &lt;20061104014159.565B52D88B1689F8@cybermedia.co.in&gt;"+
		"From: PCQuest&lt;pcqrse1@cybermedia.co.in&gt;"+
		"To: vijaydharap@yahoo.co.in"+
		"Subject: One time subscription offer"+
		"Reply-To: pcqrse1@cybermedia.co.in"+
		"Date: 04 Nov 2006 01:42:00 -0600"+
		"MIME-Version: 1.0"+
		"Content-Type: multipart/alternative;"+
		"	boundary=\"----=_NextPart_000_0012_4C606383.5AD57DD1\""+
		"Content-Length: 1711"+
		"</rawheaders></GetMessageRawHeaderResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";
		String resp = (String) parse(str);
	}
	
//	public static Object parse(String rawHeaderResponse) {
//		MessageHeaderDigester resp = null ;
//		try {
//			Digester digester = new Digester();
//			digester.setValidating(false);
//
//			digester
//			.addObjectCreate(
//					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageRawHeaderResponse",
//					thisClass);
//			digester.addObjectCreate(
//					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageRawHeaderResponse/rawheaders",
//					String.class);
//			digester.addSetNext("SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageRawHeaderResponse/rawheaders", "addHeader" );
//			
//			StringReader sr = new StringReader(rawHeaderResponse);
//			resp = (MessageHeaderDigester) digester.parse(sr);
//
//		} catch (Exception exc) {
//			exc.printStackTrace();
//		}
//		return resp;
//	}

	// this method as digestor did not work out. and i dont have time to give time 4 digestor to work out..
	public static Object parse(String rawHeaderResponse) {
		String pattern = "<rawheaders>([\\s\\S]*?)</rawheaders>";
		String headerText= (String) matcher(pattern,rawHeaderResponse).get(0);
		headerText = headerText.substring(headerText.indexOf(">")+1, headerText.indexOf("</"));
	return headerText;
}
	public static List matcher(String patternString, String searchString){
		Pattern pattern = Pattern.compile(patternString);
		Matcher matcher = pattern.matcher(searchString);
		List matcherGroup = new ArrayList() ;
//		int i = 0;
        while (matcher.find()){
        	matcherGroup.add(matcher.group());
        }
		return matcherGroup ;
	}

}